﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Media;

namespace HRMChat
{
    public partial class frmChat : DevExpress.XtraEditors.XtraForm
    {
        public frmChat()
        {
            InitializeComponent();
        }

        private StringBuilder _Conservation;
        public StringBuilder Conservation
        {
            get
            {
                if (_Conservation == null)
                {
                    _Conservation = new StringBuilder();
                }
                return _Conservation;
            }
            set { _Conservation = value; }
        }

        public string MailId
        {
            get;
            set;
        }

        public bool ReceiveFlag
        {
            get;
            set;
        }

        public int frmChatIndex
        {
            get;
            set;
        }

        public jabber.client.JabberClient jbClient
        {
            get;
            set;
        }

        private void frmChat_Load(object sender, EventArgs e)
        {
            jbClient.OnMessage += new jabber.client.MessageHandler(jbClient_OnMessage);
            txtSend.Text = "";
            txtSend.Focus();
        }

        public void jbClient_OnMessage(object sender, jabber.protocol.client.Message msg)
        {
            if (!this.ReceiveFlag)
            {
                if (msg.From.Bare == this.MailId)
                {
                    if (msg.Body != "")
                    {
                        this.WindowState = FormWindowState.Normal;
                        this.Activate();
                        Buzz(msg.Body);
                        if (string.IsNullOrEmpty(msg.Body) || msg.Body == " ")
                        {
                            AppendConversation(msg.Body);
                        }
                        else
                        {
                            string receivedMSG = msg.From.User + " nói: " + msg.Body + "\n";
                            AppendConversation(receivedMSG);
                        }

                        msg.Body = "";
                    }
                }
            }
        }

        public void AppendConversation(string str)
        {
            if (string.IsNullOrEmpty(str) || str == " ")
            {
                txtConversation.Text = this.Conservation.ToString() + @"...đang trả lời";            }
            else
            {
                if (str.Contains("<buzz/>"))
                {
                    str = str.Replace("<buzz/>", "Buzz");
                }
                this.Conservation.Append(str);
                txtConversation.Text = this.Conservation.ToString();
                //txtConversation.Focus();
                txtConversation.Document.CreatePosition(txtConversation.Text.Length - 1);
                txtConversation.ScrollToCaret();
                txtSend.Focus();
            }
        }

        private void SendMessage()
        {
            jabber.protocol.client.Message reply = new jabber.protocol.client.Message(this.jbClient.Document);
            if (txtSend.Text != "\n" && txtSend.Text != " ")
            {
                reply.Body = txtSend.Text;
                if (reply.Body != "")
                {
                    reply.To = this.MailId;
                    this.jbClient.Write(reply);
                    string sentMsg = this.jbClient.User + " nói: " + txtSend.Text + "\n";
                    AppendConversation(sentMsg);
                    txtSend.Text = "";
                    txtSend.Focus();
                }
            }
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg != 0x0010)
            {
                base.WndProc(ref m);
            }
            else
            {
                ClearConversation();
            }
        }


        private void ClearConversation()
        {
            _Conservation = null;
            if (_Conservation == null)
            {
                _Conservation = new StringBuilder();
            }
            txtConversation.Text = "";
            this.ReceiveFlag = false;
            this.Hide();
        }

        private void txtSend_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendMessage();
                this.ReceiveFlag = false;
                e.Handled = true;
            }
            else
            {
                SendMessage(null);
            }
        }

        private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
        {
            _Conservation = null;
            if (_Conservation == null)
                _Conservation = new StringBuilder();
            txtConversation.Text = "";
        }

        private void txtConversation_Enter(object sender, EventArgs e)
        {
            txtSend.Focus();
        }

        public void Buzz(string Message)
        {
            
            if (Message == "<buzz/>")
            {
                SoundPlayer simpleSound = new SoundPlayer(Application.StartupPath + @"\Media\buzz.wav");
                simpleSound.Play();
                for (int i = 0; i < 50; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        switch (j)
                        {
                            case 0:
                                this.Location = new Point(this.Left, this.Top + 7);
                                break;
                            case 1:
                                this.Location = new Point(this.Left + 7, this.Top);
                                break;
                            case 2:
                                this.Location = new Point(this.Left, this.Top - 7);
                                break;
                            case 3:
                                this.Location = new Point(this.Left - 7, this.Top);
                                break;
                            default:
                                break;
                        }
                    }

                }
            }
            else if(!string.IsNullOrEmpty(Message) && Message != " ")
            {
                if (this.Focused == false)
                {
                    SoundPlayer simpleSound = new SoundPlayer(Application.StartupPath + @"\Media\message.wav");
                    simpleSound.Play();
                }
            }
         
        }

        private void btnBuzz_Click(object sender, EventArgs e)
        {
            if (count == 0)
            {
                SoundPlayer simpleSound = new SoundPlayer(Application.StartupPath + @"\Media\buzz.wav");
                simpleSound.Play();
                this.timer1.Start();
                SendMessage("<buzz/>");
            }
        }

        private void SendMessage(string Message)
        {
            jabber.protocol.client.Message reply = new jabber.protocol.client.Message(this.jbClient.Document);
            if (Message != "\n" && Message != " ")
            {
                reply.Body = Message;
                if (reply.Body != "")
                {
                    reply.To = this.MailId;
                    this.jbClient.Write(reply);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (count == 9)
            {
                count = 0;
                this.timer1.Stop();
                this.timer1.Enabled = false;
                return;
            }
            count++;
        }
        int count = 0;
    }
}